package com.winai.launcher.view.set;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.override.dialog_global.GlobalDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：康少
 * 时间：2019/11/22 0022
 * 说明：重置设备出厂设置
 */
public class SetResetDeviceActivity extends BaseActivity {
    private Context mContext = this;

    @BindView(R.id.str_title)
    TextView strTitle;
    private GlobalDialog.Builder confirmDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_reset_device);
        ButterKnife.bind(this);
        start();
    }

    private void start() {
        String titleName = getIntent().getStringExtra("titleName");
        strTitle.setText(titleName);

        initDialog();
    }

    private void initDialog() {
        confirmDialog = new GlobalDialog.Builder(mContext)
                .setTitle("提示")
                .setContent("确定要恢复出厂设置？")
                .setConfirmText("确定")
                .setCancelText("取消")
                .setConfirmGradient(true)
                .setOnConfirmClickListener((mGlobalDialog, v) -> {
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
                        setMaster();
                    } else {
                        Intent resetIntent = new Intent("android.intent.action.FACTORY_RESET");
                        resetIntent.setPackage("android");
                        sendBroadcast(resetIntent);
                    }
                });
    }

    /**
     * 说明：恢复出厂设置
     */
    private void setMaster() {
        Intent intent = new Intent("android.intent.action.FACTORY_RESET");
        intent.setPackage("android");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        intent.putExtra("android.intent.extra.REASON", "MasterClearConfirm");
        intent.putExtra("android.intent.extra.WIPE_EXTERNAL_STORAGE", true);
        intent.putExtra("com.android.internal.intent.extra.WIPE_ESIMS", true);
        sendBroadcast(intent);
    }

    @OnClick({R.id.iv_back, R.id.iv_home, R.id.tv_reset})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_home:
                //模拟HOME
                Intent it = new Intent(Intent.ACTION_MAIN);
                it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                it.addCategory(Intent.CATEGORY_HOME);
                mContext.startActivity(it);
                break;
            case R.id.tv_reset:
                confirmDialog.show();
                break;
        }
    }
}
